package com.ukao.steward.base;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.listener.DialogSubmitListener;
import com.ukao.steward.retrofit.ApiClient;
import com.ukao.steward.retrofit.ApiStores;
import com.ukao.steward.util.DensityUtils;

import org.greenrobot.eventbus.EventBus;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ukaodev on 2017/6/2.
 */

public  abstract class BaseDialogFragment extends DialogFragment{
    protected Activity activity;
    protected  Resources resources;
    public DialogSubmitListener mDialogSubmitListener;
    private static final String MARGIN = "margin";
    private static final String WIDTH = "width";
    private static final String HEIGHT = "height";

    private static final String BOTTOM = "show_bottom";
    private static final String CANCEL = "out_cancel";
    private static final String ANIM = "anim_style";
    private static final String LAYOUT = "layout_id";
    @StyleRes
    private int animStyle;
    private boolean showBottom;//是否底部显示
    private int margin;//左右边距
    private int width;//宽度
    private int height;//高度
    private boolean outCancel = true;//是否点击外部取消
    private boolean  isWithWrap=true;//设置是否 有多宽显示多宽
    private boolean showTop;

    public Resources getResource() {
        return resources;
    }


    public void setOnSubmitClickListener(DialogSubmitListener mDialogSubmitListener) {
        this.mDialogSubmitListener = mDialogSubmitListener;
    }

    protected abstract void initView();
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recoverData(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_FullScreen);
        resources = getActivity().getResources();
        activity = getActivity();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }


    /**
     * 恢复数据
     * @param savedInstanceState
     */
    private void recoverData(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {

            margin = savedInstanceState.getInt(MARGIN);
            width = savedInstanceState.getInt(WIDTH);
            height = savedInstanceState.getInt(HEIGHT);
            showBottom = savedInstanceState.getBoolean(BOTTOM);
            outCancel = savedInstanceState.getBoolean(CANCEL);
            animStyle = savedInstanceState.getInt(ANIM);

        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window != null) {
            //设置dialog进入、退出的动画
            window.setWindowAnimations(animStyle);
            WindowManager.LayoutParams lp = window.getAttributes();
            //调节灰色背景透明度[0-1]，默认0.5f
            //是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM;
            }
            if (showTop){
                lp.gravity = Gravity.TOP;
            }


            //设置dialog宽度
            if (isWithWrap){
                lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            }else  {
                if (width == 0){ /**/

                    lp.width = DensityUtils.getScreenW() - 2 * DensityUtils.dip2px( margin);
                } else {
                    lp.width =width;
                }
            }



            //设置dialog高度
            if (height == 0) {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.height = height;
            }

            window.setAttributes(lp);
        }
        setCancelable(outCancel);
    }



    /**
     * 保存数据
     *
     * @param outState
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(MARGIN, margin);
        outState.putInt(WIDTH, width);
        outState.putInt(HEIGHT, height);
        outState.putBoolean(BOTTOM, showBottom);
        outState.putBoolean(CANCEL, outCancel);
        outState.putInt(ANIM, animStyle);

    }


    private CompositeSubscription mCompositeSubscription;

    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }

    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    public void onUnsubscribe() {

        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onUnsubscribe();
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }



    protected String getText(TextView txt) {
        String message = txt.getText().toString().trim();

        return message;
    }


    protected void initListener() {

    }
    protected  boolean useEventBus(){
        return false;
    }




    public BaseDialogFragment setMargin(int margin) {
        this.margin = margin;
        return this;
    }

    public BaseDialogFragment setWidth(int width) {
        this.width = width;
        return this;
    }

    public BaseDialogFragment setHeight(int height) {
        this.height = height;
        return this;
    }


    public BaseDialogFragment setShowTop(boolean showTop) {
        this.showTop = showTop;
        return this;
    }

    public BaseDialogFragment setShowBottom(boolean showBottom) {
        this.showBottom = showBottom;
        return this;
    }

    public BaseDialogFragment setIsWithWrap(boolean isWithWrap) {
        this.isWithWrap = isWithWrap;
        return this;
    }

    public BaseDialogFragment setOutCancel(boolean outCancel) {
        this.outCancel = outCancel;
        return this;
    }


}
